src="jquery.js";
src="public.js";

window.onload = function () {
    //test();
    // init();
    //pop();
}

function ofl() {
    alert("dsddd");
}

//身份输入
function pop() {
    var input = prompt("昵称");
    if(input == "")
        pop();
    else
        who(input);
}

//
function who(input) {

}

//提交评论
function submit() {
    var data = document.getElementById('submit').value;
    alert(data);

    sendStr("/commentAdd" , data , function (dd) {
        alert(dd);
        document.getElementById('res').innerHTML="";
        analysis(dd);
    })
}

//删除评论
function comments(object) {
    var dd = object.id;
    alert(dd);
    sendStr("/commentDel", dd , function (res) {
        alert(res);
        document.getElementById('res').innerHTML="";
        analysis(res);
    });
}

//初始化
function init() {
    //alert("js is ok");
    var data = 'hello , there .';
    //用回调机制来解决Ajax异步的问题
    sendStr("/init", data , function (res){
        alert("success");
        analysis(res);
    });
}
//解析返回值
function analysis(response) {
    alert("analysis")
    for(var i in response){
        //表示遍历数组，而i表示的是数组的下标值，
        //result[i]表示获得第i个json对象即JSONObject
        //result[i]通过.字段名称即可获得指定字段的值

        //alert(response[i].name);

        var newsp=document.createElement("p");
        newsp.id = response[i].id;
        newsp.setAttribute("onclick","comments(this)")
        document.getElementById('res').appendChild(newsp);

        var idd , item , info;
        idd = 'id'+i;
        //item = '<span id=id'+i+'></span>';
        info = response[i].id;
        rewrite(response[i].id , idd , info);

        idd = 'name'+i;
        //item = '<p id=name'+i+'></p>';
        info = response[i].name;
        rewrite(response[i].id , idd , info);

        idd = 'tel'+i;
        item = '<p id=tel'+i+'></p>';
        info = response[i].tel;
        rewrite(response[i].id , idd , info);
        //rewrite(idd , item , info);

        $('div').append('<br>');
    }
}
//动态绘制html页面
function rewrite(i,idd,info) {
    var newspan=document.createElement("span");
    newspan.id = idd;
    newspan.setAttribute("value",info);
    document.getElementById(i).appendChild(newspan);
    $('#'+idd).text(info);
// $('div').append(item);
// $('div').append('&emsp;');
// //会保留标签，但是换行
// $(idd).text(info);
}

//功能函数，进行异步通信
function sendStr(url , str , callback) {
    $.ajax({
        type: "POST",
        url: url,
        contentType:"application/json",
        data: str,
        dataType : "json",
        success : function (response) {
            alert("success in sendStr");
            callback(response);
        },
        error:function(response){
            callback(response);
        }
    });
}
function sendJson(url , json , callback) {
    $.ajax({
        type: "POST",
        url: url,
        contentType:"application/json",
        data: JSON.stringify(json),
        dataType : "json",
        success : function (response) {
            callback(response);
        },
        error:function(response){
            callback(response);
        }
    });
}


